package com.uni.sofia.reader.utils.test
{
	import com.uni.sofia.reader.model.ApplicationManager;
	import com.uni.sofia.reader.utils.UrlParser;
	
	import flexunit.framework.Assert;
	
	import org.flexunit.asserts.assertEquals;
	
	/**
	 * Tests the <code>UrlParser</code> utility class.
	 */ 
	public class UrlParserTest
	{		
		private var urlParser:UrlParser;
		
		[Before]
		public function setUp():void
		{
			urlParser = new UrlParser();
		}
		
		[Test]
		public function testParseNull():void
		{
			assertEquals(ApplicationManager.DEFAULT_STATE, urlParser.parseState(null));
		}
		
		[Test]
		public function testParseRandomStringsToState():void
		{
			//when invalid url is passed, the application will go in its default stata
			assertEquals(ApplicationManager.DEFAULT_STATE, urlParser.parseState('#{}'));
			assertEquals(ApplicationManager.DEFAULT_STATE, urlParser.parseState('#{abv.bg}'));
			assertEquals(ApplicationManager.DEFAULT_STATE, urlParser.parseState('#{google@gmail.com}'));
		}
		
		[Test]
		public function parseUnfinishedState():void
		{
			assertEquals(ApplicationManager.DEFAULT_STATE, urlParser.parseState('#{lib'));
		}
		
		[Test]
		public function testParseRealShortUrlToState():void
		{
			assertEquals("lib", urlParser.parseState('#{state=lib}'));
		}
		
		[Test]
		public function testGetState1():void
		{
			assertEquals("lib", urlParser.parseState('#{state=lib&keywords=something}'));
		}
		
		[Test]
		public function testGetState2():void
		{
			assertEquals(ApplicationManager.DEFAULT_STATE, urlParser.parseState('#{lib}'));
		}
		
		[Test]
		public function testGetState3():void
		{
			assertEquals(ApplicationManager.DEFAULT_STATE, urlParser.parseState('#'));
		}
		
		[Test]
		public function testGetState4():void
		{
			assertEquals(ApplicationManager.DEFAULT_STATE, urlParser.parseState('#{key=library}'));
		}
		
		[Test]
		public function testParseFirst():void
		{
			assertEquals('state', urlParser.parseFirst('state=smoke'));
			assertEquals('lib', urlParser.parseFirst('lib=smoke'));
		}
		
		[Test]
		public function testParseSecond():void
		{
			assertEquals('smoke', urlParser.parseSecond('state=smoke'));
			assertEquals('smoke', urlParser.parseSecond('lib=smoke'));
		}
		
		[Test]
		public function validateParseProperty():void
		{
			assertEquals('mock', urlParser.parseProperty('#{state=mock}', 'state'));
			assertEquals('magazine', urlParser.parseProperty('#{state=mock&key=magazine}', 'key'));
		}
		
		[Test]
		public function parseSimpleJsonToUrl():void
		{
			var json:Object = {};
			json.state='mock';
			assertEquals('#{state=mock}', urlParser.jsonToUrl(json));
		}
		
		[Test]
		public function parseComplexJsonToUrl():void
		{
			var json:Object = {};
			json.state='lib';
			json.magazine='Times';
			assertEquals('lib', urlParser.parseState(urlParser.jsonToUrl(json)));
			assertEquals('Times', urlParser.parseProperty(urlParser.jsonToUrl(json),'magazine'));
			json.view='imagesonly';
			assertEquals('lib', urlParser.parseState(urlParser.jsonToUrl(json)));
		}
	}
}